package wyp.library.ui.listview;

import wyp.library.R;
import wyp.library.ui.view.WypSearchBar;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import android.view.View.OnKeyListener;

public class WypSearchListView extends RelativeLayout implements OnKeyListener {

	public interface WypSearchListViewDelegate {
		public void onSearch(WypSearchListView __listView, String __searchStr);
	}

	public WypSearchBar searchBar;
	public WypBaseListView listView;
	public WypSearchListViewDelegate delegate;

	public WypSearchListView(Context _context) {
		super(_context);
		this._init(_context);
	}

	public WypSearchListView(Context _context, AttributeSet _attrs) {
		super(_context, _attrs);
		this._init(_context);
	}

	public WypSearchListView(Context _context, AttributeSet _attrs, int _defStyle) {
		super(_context, _attrs, _defStyle);
		this._init(_context);
	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_ENTER) {
			InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(searchBar.searchEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			if (delegate != null) {
				delegate.onSearch(this, searchBar.searchEditText.getText().toString().trim());
			}
			return true;
		}
		return false;
	}

	private void _init(Context _context) {
		View _view = LayoutInflater.from(_context).inflate(R.layout.wyp_search_listview, this, true);
		searchBar = (WypSearchBar) _view.findViewById(R.id.wyp_search_layout);
		listView = (WypBaseListView) _view.findViewById(R.id.wyp_search_listview);
		searchBar.searchEditText.setOnKeyListener(this);
	}

	public void setAdapter(WypListViewAdapter<?> _adapter) {
		listView.setAdapter(_adapter);
	}

	// TODO:test
	private boolean mHasInit;
	private boolean mHasKeybord;
	private onKybdsChangeListener mListener;
	private int mHeight;
	public static final byte KEYBOARD_STATE_SHOW = -3;
	public static final byte KEYBOARD_STATE_HIDE = -2;
	public static final byte KEYBOARD_STATE_INIT = -1;
	private static final String TAG = WypSearchListView.class.getSimpleName();

	/**
	 * set keyboard state listener
	 */
	public void setOnkbdStateListener(onKybdsChangeListener listener) {
		mListener = listener;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		if (!mHasInit) {
			mHasInit = true;
			mHeight = b;
			if (mListener != null) {
				mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
			}
		} else {
			mHeight = mHeight < b ? b : mHeight;
		}
		if (mHasInit && mHeight > b) {
			mHasKeybord = true;
			if (mListener != null) {
				mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
			}
			Log.w(TAG, "show keyboard.......");
		}
		if (mHasInit && mHasKeybord && mHeight == b) {
			mHasKeybord = false;
			if (mListener != null) {
				mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
			}
			Log.w(TAG, "hide keyboard.......");
		}
	}

	public interface onKybdsChangeListener {
		public void onKeyBoardStateChange(int state);
	}

}
